package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.user.article.ArticleListener;

public class ArticleHomeView extends Composite {

	interface Binder extends UiBinder<Widget, ArticleHomeView> {
	}

	private static ArticleHomeView instance;
	private static final Binder binder = GWT.create(Binder.class);
	@UiField
	SideBar sidebar;
	@UiField
	SearchResultPanel searchResult;
	@UiField
	SystemTopRecommendPanel topRecommend;
	@UiField
	SystemTopEntityPanel topEntity;
	@UiField
	TextBox searchBox;
	@UiField
	HTML searchImage;

	public static ArticleHomeView getInstance() {
		if (instance == null)
			instance = new ArticleHomeView();
		return instance;
	}

	private ArticleHomeView() {
		initWidget(binder.createAndBindUi(this));
		addStyleName("topPanel");
		searchImage.setHTML("<a><img src=\"" + GWT.getModuleBaseURL()
				+ "icon_search.png\"/>" + "<a>");
	}

	@UiHandler("searchBox")
	public void onSearchBoxKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == 13)
			search();
	}

	@UiHandler("searchImage")
	public void onSearchImageClick(ClickEvent event) {
		search();
	}

	public void search() {
		String keyword = searchBox.getText();
		if (keyword != null && !keyword.isEmpty()){
			ArticleListener.getInstance().search(keyword);
		}
	}
	
}
